package kalender.backend;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.TransactionRequiredException;
import javax.persistence.UniqueConstraint;
import javax.persistence.Table;


@Entity
@Table(uniqueConstraints=@UniqueConstraint(columnNames="uname"))
public class Luser  {

	private int uid;
	private String uname;
	private String passwd;
	private String name;
    private String cg_class;
    private String cg_program;
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY )
	public int getUid() { return uid; }
	public void setUid(int id) { uid = id; }
	
	public String getUname() { return uname; }
	public void setUname(String un) { uname = un; }
	
	public String getPasswd() { return passwd; }
	public void setPasswd(String p) { passwd = p; }
	
	public String getName() { return name; }
	public void setName(String n) { name = n; }
    
    public String getCg_class() { return cg_class; }
    public void setCg_class(String c) { cg_class = c; }
    
    public String getCg_program() { return cg_program; }
    public void setCg_program(String pr) { cg_program = pr; }
	
	public String toString() {
		return "uid: " + getUid() + " uname: " + getUname() + " passwd: " + getPasswd() + " name: " + getName();
	}
	
	public static Luser find(int uid) throws BackendException {
		try {
			EntityManagerFactory factory = Persistence.createEntityManagerFactory("derp");
			EntityManager em = factory.createEntityManager();
			return em.find(Luser.class, uid);
		} catch (IllegalStateException e) {
            e.printStackTrace();
			throw new BackendException("Databasfel. Var god försök igen.", e);
		} catch (IllegalArgumentException e) {
            e.printStackTrace();
			throw new BackendException("Databasfel. Var god försök igen.", e);
		}
	}
	
	public static Luser find(String username) throws BackendException {
		try {
			EntityManagerFactory factory = Persistence.createEntityManagerFactory("derp");
			EntityManager em = factory.createEntityManager();
			Query q = em.createQuery("select t from Luser t where t.uname = ?1");
			q.setParameter(1, username);
			
			@SuppressWarnings("unchecked")
			List<Luser> list = q.getResultList();
			
			if (list != null && list.size() >= 1) {
				return list.get(0);
			} else {
				return null;
			}
		} catch (IllegalStateException e) {
            e.printStackTrace();
			throw new BackendException("Databasfel. Var god försök igen.", e);
		} catch (IllegalArgumentException e) {
            e.printStackTrace();
			throw new BackendException("Databasfel. Var god försök igen.", e);
		}
	}

	@SuppressWarnings("unchecked")
	public static List<Luser> findAll() throws BackendException {
		try {
			EntityManagerFactory factory = Persistence.createEntityManagerFactory("derp");
			EntityManager em = factory.createEntityManager();
			Query q = em.createQuery("select t from Luser t order by t.uid");
			return q.getResultList();
		} catch (IllegalStateException e) {
            e.printStackTrace();
			throw new BackendException("Databasfel. Var god försök igen.", e);
		} catch (IllegalArgumentException e) {
            e.printStackTrace();
			throw new BackendException("Databasfel. Var god försök igen.", e);
		}
	}
	
	public void saveNew() throws BackendException {
		try {
			EntityManagerFactory factory = Persistence.createEntityManagerFactory("derp");
			EntityManager em = factory.createEntityManager();
			
			em.getTransaction().begin();
			
			em.persist(this);
			
			em.getTransaction().commit();
			
			em.close();
			factory.close();
		} catch (EntityExistsException e) {
            e.printStackTrace();
			throw new BackendException("Databasfel. Var god försök igen.", e);			
		} catch (IllegalStateException e) {
            e.printStackTrace();
			throw new BackendException("Databasfel. Var god försök igen.", e);			
		} catch (IllegalArgumentException e) {
            e.printStackTrace();
			throw new BackendException("Databasfel. Var god försök igen.", e);			
		} catch (TransactionRequiredException e) {
            e.printStackTrace();
			throw new BackendException("Databasfel. Var god försök igen.", e);		
		}
	}
	
	public void saveUpdate() throws BackendException {
		try {
			EntityManagerFactory factory = Persistence.createEntityManagerFactory("derp");
			EntityManager em = factory.createEntityManager();
			
			em.getTransaction().begin();
			
			em.merge(this);
			
			em.getTransaction().commit();
		} catch (EntityExistsException e) {
            e.printStackTrace();
			throw new BackendException("Databasfel. Var god försök igen.", e);			
		} catch (IllegalStateException e) {
            e.printStackTrace();
			throw new BackendException("Databasfel. Var god försök igen.", e);			
		} catch (IllegalArgumentException e) {
            e.printStackTrace();
			throw new BackendException("Databasfel. Var god försök igen.", e);			
		} catch (TransactionRequiredException e) {
            e.printStackTrace();
			throw new BackendException("Databasfel. Var god försök igen.", e);		
		}
	}
	
	public static void delete(int uid) throws BackendException{
		try {
			EntityManagerFactory factory = Persistence.createEntityManagerFactory("derp");
			EntityManager em = factory.createEntityManager();
			
			em.getTransaction().begin();
			
			Luser l = em.find(Luser.class, uid);
			em.remove(l);
			
			em.getTransaction().commit();
			
			em.close();
			factory.close();
		} catch (EntityExistsException e) {
            e.printStackTrace();
			throw new BackendException("Databasfel. Var god försök igen.", e);			
		} catch (IllegalStateException e) {
            e.printStackTrace();
			throw new BackendException("Databasfel. Var god försök igen.", e);			
		} catch (IllegalArgumentException e) {
            e.printStackTrace();
			throw new BackendException("Databasfel. Var god försök igen.", e);			
		} catch (TransactionRequiredException e) {
            e.printStackTrace();
			throw new BackendException("Databasfel. Var god försök igen.", e);		
		}
	}
}
